package controller;

import beans.HttpResultList;
import dao.CompanyDao;
import net.sf.json.JSONObject;
import portal.controller.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.List;

public class CompanyServlet extends BaseServlet {
	@Autowired
    private CompanyDao companyDao;
    private HttpResultList result;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String operateType = request.getParameter("operateType");
        try {
            switch (operateType){
                case "add":
                    addCompany(request,response);
                    break;
                case "delete":
                    deleteCompany(request,response);
                    break;
                case "alter":
                    alterCompany(request,response);
                    break;
                case "showAll":
                    showAllCompany(request,response);
                    break;
                case "showDetail":
                    showCompanyDetail(request,response);
                    break;
            }
        } catch (NullPointerException e) {
            e.printStackTrace();
            result.setResult(false);
            result.setMessage("POST参数错误");
            response.getWriter().print(JSONObject.fromObject(result).toString());
        } catch (IOException e) {
            e.printStackTrace();
            result.setResult(false);
            result.setMessage("服务器错误");
        }catch (NumberFormatException e) {
            e.printStackTrace();
            result.setResult(false);
            result.setMessage("POST数字错误");
        }
        response.getWriter().print(JSONObject.fromObject(result).toString());
    }
    private void addCompany(HttpServletRequest request, HttpServletResponse response) throws NullPointerException, IOException {
        String name = request.getParameter("companyName");
        result = new HttpResultList();
        result.setResult(companyDao.addCompany(name));
    }
    private void deleteCompany(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("companyId");
        result = new HttpResultList();
        result.setResult(companyDao.deleteCompany(id));
    }
    private void alterCompany(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String companyId = request.getParameter("companyId");
        String name = request.getParameter("companyName");
        result = new HttpResultList();
        result.setResult(companyDao.alterCompany(companyId, name));
    }
    private void showAllCompany(HttpServletRequest request, HttpServletResponse response) {
        Integer onePageCount = Integer.parseInt(request.getParameter("onePageCount"));
        Integer currentPage = Integer.parseInt(request.getParameter("currentPage"));
        result = new HttpResultList();
        List<Object> companyList = companyDao.showAllCompany(onePageCount, currentPage);
        result.setResult(companyList.size()>0);
        result.setDatas(companyList);
        if(onePageCount!=0&&currentPage!=0){
            result.setMessage(companyDao.companyPageNum(onePageCount)+"");
        }
    }
    private void showCompanyDetail(HttpServletRequest request, HttpServletResponse response) {
        String companyId = request.getParameter("companyId");
        result = new HttpResultList();
        List<Object> company = companyDao.showOneCompany(companyId);
        result.setResult(company.size()>0);
        result.setDatas(company);
    }
}
